import { _decorator, Component, instantiate, math, Node, Prefab } from 'cc';
import { globalConfig } from './globalConfig';
const { ccclass, property } = _decorator;

@ccclass('createFood')
export class createFood extends Component {
    @property({ type: Prefab }) food = null

    start() {
        this.createFood()
    }

    update(deltaTime: number) {

    }

    createFood() {
        let num = Math.random() * 5
        for (let index = 0; index < num; index++) {
            globalConfig.foodNumber++
            const food = instantiate(this.food)
            food.parent = this.node
            food.setPosition(this.generateRandomNumber(), 0.5, this.generateRandomNumber())
        }
    }
    generateRandomNumber(): number {
        let randomNumber: number;
        do {
            randomNumber = Math.floor(Math.random() * 15) - 7; // 生成-7到7的随机数
        } while (randomNumber === 0);
    
        return randomNumber;
    }
    
}


